package com.cjt.chat.controller;

import com.cjt.chat.pojo.Message;
import com.cjt.chat.pojo.User;
import com.cjt.chat.service.MessageService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.asleepyfish.config.ChatGPTProperties;
import io.github.asleepyfish.service.OpenAiProxyService;
import io.github.asleepyfish.util.OpenAiUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @author cjt
 * @since 2024/3/28
 */
@RestController
@CrossOrigin
public class MessageController {
    @Resource
    private MessageService messageService;

    @GetMapping(path = "/getMessageHistory")
    public List<Message> getMessageHistory(Integer friendId, Integer userId, Integer page, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user != null) {
            List<Message> messageHistory = messageService.getMessageHistory(friendId, userId, page);
            return messageHistory;
        }
        return null;
    }

    @PostMapping(path = "/AIChat")
    public void AIChat(String content, HttpServletResponse response, HttpSession session) throws IOException {
        User user = (User) session.getAttribute("user");
        if (user != null) {
            response.setContentType("text/event-stream");
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Cache-Control", "no-cache");
            OpenAiUtils.createStreamChatCompletion(content, user.getUserid().toString(), response.getOutputStream());
            return;
        }
        response.setContentType("text/event-stream");
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Cache-Control", "no-cache");
        response.getWriter().println("非法用户!");
    }

    @GetMapping(path = "/getSystemMessage")
    public List<Message> getSystemMessage(Integer userId) {
        return messageService.getSystemMessage(userId);
    }

    @GetMapping(path = "/deleteMessageById")
    public void deleteMessageById(Integer messageId, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user != null) {
            messageService.deleteMessageById(messageId);
        }
    }

    @RequestMapping(path = "/exportMessage", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<byte[]> exportMessage(Integer friendId, HttpSession session) throws JsonProcessingException {
        User user = (User) session.getAttribute("user");
        if (user != null) {
            List<Message> messageHistory = messageService.getAllMessageHistory(friendId, user.getUserid());
            // 设置响应头信息
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            headers.setContentDispositionFormData("filename", user.getUserid() + "-" + friendId + "message.json");

            byte[] messageBytes = new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsBytes(messageHistory);

            // 返回 JSON 数据
            return new ResponseEntity<>(messageBytes, headers, HttpStatus.OK);
        }
        return null;
    }
}
